package com.scaffold.upload.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import wangjg.commons.character.DigestUtil;

import com.scaffold.commons.exception.BizRTException;
import com.scaffold.commons.file.FileDetail;
import com.scaffold.commons.file.FileIO;
import com.scaffold.upload.entity.FileInfo;

/**
 * 写文件都走这个类,保持一致性
 * @author wangjg
 *
 */
@Service
public class FileService {
	@Autowired
	private FileIO fileIO;
	@Autowired
	private FileInfoService fileInfoService;

	public void save(FileInfo fileInfo, InputStream in) throws IOException{
		//保存文件信息
		this.fileInfoService.save(fileInfo);
		//保存文件内容
		this.fileIO.save(fileInfo.getFilePath(), in);
		
		//设置文件信息
		this.setFileInfo(fileInfo);
		//更新文件信息
		this.fileInfoService.update(fileInfo);
	}
	
	private void setFileInfo(FileInfo fileInfo) throws IOException{
		Long fileSize = this.fileIO.getLength(fileInfo.getFilePath());
		String hashCode = this.getHashCode(fileInfo.getFilePath());
		fileInfo.setFileSize(fileSize);
		fileInfo.setHashCode(hashCode);
	}

	private String getHashCode(String path) throws IOException{
		InputStream in = null;
		try{
			in = this.fileIO.getInputStream(path);
			String code = DigestUtil.getFileHash(in);
			return code;
		}finally{
			if(in!=null){
				in.close();
			}
		}
	}
	
	public void move(String srcPath, String destPath) throws IOException{
		boolean success = this.fileIO.move(srcPath, destPath);
		if(!success){
			throw new BizRTException("移动失败!");
		}
		FileInfo fileInfo = this.fileInfoService.findByPath(srcPath);
		fileInfo.setFilePath(destPath);
		this.fileInfoService.save(fileInfo);
	}

	public void delete(Long id) throws IOException {
		FileInfo fileInfo = fileInfoService.getFileInfo(id);
		boolean success = this.fileIO.delete(fileInfo.getFilePath());
		if(!success){
			throw new BizRTException("删除失败!");
		}
		this.fileInfoService.delete(fileInfo);
	}
	
	public void delete(String filePath) throws IOException {
		FileInfo fileInfo = fileInfoService.findByPath(filePath);
		boolean success = this.fileIO.delete(filePath);
		if(!success){
			throw new BizRTException("删除失败!");
		}
		if(fileInfo!=null){
			this.fileInfoService.delete(fileInfo);
		}
	}
	
	public boolean exists(String path) throws IOException{
		return this.fileIO.exists(path);
	}
	
	public long getFileLength(String filePath) throws IOException{
		return this.fileIO.getLength(filePath);
	}

	public void load(String filePath, OutputStream outputStream) throws IOException {
		this.fileIO.load(filePath, outputStream);
	}

	public InputStream getInputStream(String filePath) throws IOException {
		return this.fileIO.getInputStream(filePath);
	}

	public OutputStream getOutputStream(String sizeFilePath) throws IOException {
		return this.fileIO.getOutputStream(sizeFilePath);
	}

	public List<String> getChildren(String path) throws IOException {
		return this.fileIO.getChildren(path);
	}

	public FileDetail getFileStatus(String path) throws IOException {
		return this.fileIO.getFileStatus(path);
	}
	
}
